﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using InkFx.WinControl.Core;

namespace InkFx.WinControl
{
    public class SplitterForm : NoneForm
    {
        public SplitterForm(Rectangle rectRange, Rectangle rectSplit)
        {
            base.BackColor = Color.Black; //Win32API.Transparent; //
            base.Opacity = 0.5;
            base.Enabled = false;

            this.RectRange = rectRange;
            this.RectStart = GetSplitRect(rectSplit);

            //if (this.RectStart.Width >= 10) Console.WriteLine("S: " + this.RectStart);  //todo
            //Console.WriteLine("S: " + this.RectStart);
            //Console.WriteLine("R: " + this.RectRange);
        }

        public static SplitterForm BeginSplitter(System.Windows.Forms.Form owner, Rectangle rectRange, Rectangle rectSplit)
        {
            SplitterForm form = new SplitterForm(rectRange, rectSplit);
            form.Show(owner);
            return form;
        }


        public Rectangle RectRange { get; protected set; }
        public Rectangle RectStart { get; protected set; }

        public void ShowSplitter(Rectangle rectSplit)
        {
            this.Bounds = GetSplitRect(rectSplit);
        }
        public bool EndSplitter()
        {
            var rectN = this.Bounds;
            var rectS = this.RectStart;
            var result = rectN != rectS;
            //Console.WriteLine("End: " + result + " : " + rectS + " > " + rectN);

            this.Close();
            this.Dispose();

            return result;
        }

        public Rectangle GetSplitRect(Rectangle rect)
        {
            var rectM = this.RectRange;
            if (rectM.Width >= 1 || rectM.Height >= 1)
            {
                if (rect.X < rectM.X) rect.X = rectM.X;
                if (rect.Y < rectM.Y) rect.Y = rectM.Y;
                if (rect.Right > rectM.Right) rect.X = rectM.X + rectM.Width - rect.Width;
                if (rect.Bottom > rectM.Bottom) rect.Y = rectM.Y + rectM.Height - rect.Height;
            }

            if (rect.Width <= 0) rect.Width = 3;
            if (rect.Height <= 0) rect.Height = 3;
            return rect;
        }


        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            this.Bounds = this.RectStart;
            //Console.WriteLine("OnShown");
        }
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            if (m.Msg == Win32Msg.WM_NCHITTEST) { m.Result = (IntPtr)(-1L); return; }
            base.WndProc(ref m);
        }




        #region  作 废 的 函 数

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            //Win32API.DrawSplitHelper(this.Handle, new Rectangle(0, 0, this.Width, this.Height));
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //e.Graphics.Clear(Color.PaleVioletRed);
            //Win32API.DrawSplitHelper(this.Handle, new Rectangle(0, 0, this.Width, this.Height));
        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
            //Win32API.DrawSplitHelper(this.Handle, new Rectangle(0, 0, this.Width, this.Height));
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            //Win32API.DrawSplitHelper(this.Handle, new Rectangle(0, 0, this.Width, this.Height));
        }

        #endregion


    }
}
